/**
 * @file StreamPlayer.h
 *
 * This file defines the StreamPlayer interface.
 *
 * @author Luc Fourestier
 * @date 2009
 */

#ifndef STREAMPLAYER_H_
#define STREAMPLAYER_H_

#include "Global.h"

#include "StreamAnalyser.h"

#include <netinet/in.h>
#include <pthread.h>

#define SP_MAX_NAME_LENGTH MAX_NAME_LENGTH

// Errors
#define SP_ERROR_BAD_PARAM       1	//!< Incorrect parameters
#define SP_ERROR_CANNOT_ALLOCATE 2	//!< Cannot create/allocate object required by the StreamPlayer
#define SP_ERROR_CANNOT_DUMP	 3	//!< Cannot dump into a file (file write error)
#define SP_ERROR_CANNOT_OPEN_FILE 4 //!< Cannot open file
#define SP_ERROR_SOCKET_ERROR    5  //!< Cannot open/read/write from sockets
#define SP_ERROR_NOT_SUPPORTED   6  //!< Output type is not supported yet


/**
 * Specify the option to be used.
 *
 */
typedef struct SP_Option_s
{
	InOutType_t OutputType;						//!< The output type
	char FileName[SP_MAX_NAME_LENGTH];  		//!< The name of the output file where to store the stream
	char SendAddress[IP_ADDR_LENGTH];  		//!< The transmission address
	unsigned int SendPort;  				    //!< The transmission port
	CircBuff_t * CB;							//!< The circular buffer where to push the stream chunks
	struct timeval ReferenceTime; 				//!< The reference time for timestamps
} SP_Option_t;

/**
 * The StreamPlayer info structure
 */
typedef struct SP_StreamPlayer_s
{
	SP_Option_t Options;						//!< The option for this StreamPlayer
	FILE * StorageFile; 						//!< Storage file pointer
	struct sockaddr_in RemoteUDPAddr;			//!< The remote UDP address
	int Socket;								//!< The UDP transmission socket
	unsigned char ThreadLoop;					//!< The infinite loop of the analyzer thread
	pthread_t PlayerThreadID;					//!< The StreamPlayer thread ID
	unsigned long long TotalBytePlayed;		    //!< The number of bytes played out
	unsigned long long TotalDumpError;		    //!< The number of erroneous dumps to file
	unsigned long long TotalUDPError;		    //!< The number of erroneous UDP transmission

#ifdef DEBUG_SP_PROFILING
	struct timeval TotalPassDuration;			//!< The sum of all pass duration (a pass is one thread round)
	unsigned long long passCount;				//!< The number of pass
	unsigned long long passError;				//!< The number of pass error
#endif
} SP_StreamPlayer_t;

/**
 * @brief Create the StreamPlayer
 */
int SP_Create(SP_StreamPlayer_t ** SP_p, SP_Option_t Options);

/**
 * @brief Delete the StreamPlayer
 */
int SP_Delete(SP_StreamPlayer_t * SP);

/**
 * @brief Print the StreamPlayer
 */
int SP_Print(SP_StreamPlayer_t * SP, char * Name, int Level, FILE * Output);

#endif /* STREAMPLAYER_H_ */
